<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Created by PhpStorm.
 * User: liunwcj
 * Date: 15-5-29
 * Time: 下午5:37
 * think: http://www.cnblogs.com/mackxu/archive/2012/08/06/2625144.html
 */
class User extends CI_Controller
{
    function __construct() {
        parent::__construct();
        $this->load->helper(array('url', 'form'));
        $this->load->model('User_model');
        $this->load->database();
        $this->load->library('form_validation');
    }
    function index() {
        $this->login();
    }
    /**login**/
    function login() {
        $data['title']='用户登录';
        $this->_username = $this->input->post('username');
        $config = array(
            array(
                'field'=>'username',
                'label'=>'用户名',
                'rules'=>'trim|required|callback_username_check'
            ),
            array(
                'field'=>'password',
                'label'=>'密码',
                'rules'=>'trim|required|callback_password_check'
            )
        );
        $this->form_validation->set_rules($config);
        if($this->form_validation->run() == false){
            $data['error']=validation_errors();
            if($data['error']){
                $this->load->view('public/header',$data);
            }else {
                $this->load->view('public/header',['title'=>'用户登录']);
            }
            $this->load->view('user/login');
            $this->load->view('public/footer');
        }else{
            echo $this->_username.'<br/>';
            echo '登录成功！';
            $this->load->view('public/header',$data);
            $this->load->view('public/footer');
        }
    }
    // 自定义验证用户名 callback_username_check
    function username_check($username){
       if($this->User_model->get_username($username)){
           return true;
       }else{
           $this->form_validation->set_message('username_check','用户名不存在！');
           return false;
       }
    }
    // 自定义验证密码 callback_password_check
    function password_check($pwd){
        // $pwd = $this->salt.$pwd;
        if($this->User_model->pwd_check($this->_username,$pwd)){
            return true;
        }else{
            $this->form_validation->set_message('password_check','用户名或密码不正确');
            return false;
        }
    }
    /**login End**/

    /**reg**/
    function reg() {
        $data['title']='用户注册';
        $this->load->view('public/header',$data);
        $this->load->view('user/reg');
        $this->load->view('public/footer');
    }

    function add_item() {
        $config=array(
            array(
                'field'=>'username',
                'label'=>'用户名',
                'rules'=>'required'
            ),
            array(
                'field'=>'pwd',
                'label'=>'设置密码',
                'rules'=>'required|callback_pwd_check'
            ),
            array(
                'field'=>'pwd2',
                'label'=>'确认密码',
                'rules'=>'required|callback_pwd_check'
            ),
            array(
                'field'=>'email',
                'label'=>'邮箱',
                'rules'=>'required|valid_email'
            )
        );

        $this->form_validation->set_rules($config);
        $this->_pwd = $this->input->post('pwd');
        if($this->form_validation->run() == false){
            $data['title']='用户注册提示';
            $data['error']=validation_errors();
            $this->load->view('public/header',$data);
            $this->load->view('user/reg');
            $this->load->view('public/footer');
        }else{
            $username = $this->input->post('username');
            $data = array('username'=>$username,'password'=>$this->input->post('pwd'),'email'=>$this->input->post('email'));
//          插入数据
            $this->User_model->set_user($data);
            echo $username;
            echo '注册成功！';
        }
    }
    function pwd_check($pwd){
        if($this->_pwd != $pwd){
            $this->form_validation->set_message('pwd_check','2次密码不一致！');
            return false;
        }
    }
}
